package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.tiny.persistence.domain.WxUser;
import com.tiny.persistence.repository.plus.WxUserRepoPlus;

/**
 * 微信用户
 * 
 * @ClassName: WxUserRepoImpl
 * @Description: TODO
 * @author:
 * @date: 2014年3月28日
 * 
 */
public class WxUserRepoImpl extends BaseRepoImpl<WxUser> implements
		WxUserRepoPlus {

	private static Logger logger = Logger.getLogger(WxUserRepoImpl.class);

	public WxUser findByUsernameAndPassword(String username, String password) {

		logger.info("RepoImpl username=" + username + " pass=" + password);

		if (username.equals("wushen")) {
			WxUser WxUser = new WxUser();
			WxUser.setUsername("wushen");
			return WxUser;
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WxUser> getListByCondition(Map<String, Object> condition) {

		StringBuffer sqlquery = new StringBuffer();
		sqlquery.append("from WxUser as a where 1=1 ");

		// 查询条件
		String username = null;
		if (condition.get("username") != null) {
			username = String.valueOf(condition.get("username"));
		}
		if (username != null) {
			sqlquery.append(" and a.username like :username");
		}
		// 查询条件
		String mobilePhone = null;
		if (condition.get("mobilePhone") != null) {
			mobilePhone = String.valueOf(condition.get("mobilePhone"));
		}
		if (mobilePhone != null) {
			sqlquery.append(" and a.mobilePhone like :mobilePhone");
		}
		Integer roleId = null;
		if (condition.get("roleId") != null) {
			roleId = Integer.valueOf(condition.get("roleId").toString());
		}
		if (roleId != null) {
			if (roleId.intValue() == 0) {
				sqlquery.append(" and a.wxRole.id<>1");
			} else {
				sqlquery.append(" and a.wxRole.id=:roleId");
			}
		}
		sqlquery.append(" order by a.createTime desc ");
		Query query = em.createQuery(sqlquery.toString());
		if (username != null) {
			query.setParameter("username", "%" + username + "%");
		}
		if (mobilePhone != null) {
			query.setParameter("mobilePhone", "%" + mobilePhone + "%");
		}
		if (roleId != null && roleId.intValue() != 0) {
			query.setParameter("roleId", roleId);
		}
		UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
		query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
		query.setMaxResults(uc.getPageSize());
		List<WxUser> list = query.getResultList();

		logger.info("======= " + list);

		return list;
	}

	@Override
	public int getCountByCondition(Map<String, Object> condition) {

		StringBuffer sqlquery = new StringBuffer();
		sqlquery.append("select count(*) from WxUser as a where 1=1 ");

		// 查询条件
		String username = null;
		if (condition.get("username") != null) {
			username = String.valueOf(condition.get("username"));
		}
		if (username != null) {
			sqlquery.append(" and a.username like :username");
		}
		// 查询条件
		String mobilePhone = null;
		if (condition.get("mobilePhone") != null) {
			mobilePhone = String.valueOf(condition.get("mobilePhone"));
		}
		if (mobilePhone != null) {
			sqlquery.append(" and a.mobilePhone like :mobilePhone");
		}

		Integer roleId = null;
		if (condition.get("roleId") != null) {
			roleId = Integer.valueOf(condition.get("roleId").toString());
		}
		if (roleId != null) {
			if (roleId.intValue() == 0) {
				sqlquery.append(" and a.wxRole.id<>:roleId");
			} else {
				sqlquery.append(" and a.wxRole.id=:roleId");
			}
		}
		Query query = em.createQuery(sqlquery.toString());
		if (username != null) {
			query.setParameter("username", "%" + username + "%");
		}
		if (mobilePhone != null) {
			query.setParameter("mobilePhone", "%" + mobilePhone + "%");
		}
		if (roleId != null) {
			query.setParameter("roleId", roleId);
		}
		Object obj = query.getSingleResult();

		logger.info("======= count=" + obj);

		return Integer.parseInt(obj.toString());
	}

	@SuppressWarnings("unchecked")
	public WxUser findByUsername(String username) {

		logger.info("RepoImpl username=" + username);

		Query query = em
				.createQuery("from WxUser as au where au.username=:username");
		query.setParameter("username", username);
		List<WxUser> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public WxUser getByOpenId(String openId) {
		Query query = em
				.createQuery("from WxUser as au where au.openId=:openId");
		query.setParameter("openId", openId);
		@SuppressWarnings("unchecked")
		List<WxUser> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("请关注,改openId对应的微信用户为空:openid=" + openId);
		}
		return null;
	}

	public WxUser findByMobilePhone(String bankUserPhone) {
		Query query = em
				.createQuery("from WxUser as au where au.mobilePhone=:mobilePhone");
		query.setParameter("mobilePhone", bankUserPhone);
		@SuppressWarnings("unchecked")
		List<WxUser> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
